﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QQ.Framework;
using YinLongTool.Number;


namespace YinLongTool.Extensions
{
    /// <summary>
    /// 进制转换帮助类
    /// </summary>
    public class BinaryExtension
    {  /// <summary>
        /// 十进制转任意进制
        /// </summary>
        /// <param name="num"></param>
        /// <param name="bin">进制</param>
        /// <returns></returns>
        public static string ToBinary(int num, int bin)
        {
            var nf = new NumberFormater(bin);
            return nf.ToString(num);
        }

        /// <summary>
        /// 十进制转任意进制
        /// </summary>
        /// <param name="num"></param>
        /// <param name="bin">进制</param>
        /// <returns></returns>
        public static string ToBinary(long num, int bin)
        {
            var nf = new NumberFormater(bin);
            return nf.ToString(num);
        }
        /// <summary>
        /// 任意进制转十进制
        /// </summary>
        /// <param name="str"></param>
        /// <param name="bin">进制</param>
        /// <returns></returns>
        public static long FromBinary(string str, int bin)
        {
            var nf = new NumberFormater(bin);
            return nf.FromString(str);
        }

        /// <summary>
        ///  Hex字符串转byte[]数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] HexStringToByteArray(string hexString)
        {
            hexString = hexString.Replace(" ", "").Replace("\n", "");
            if (hexString.Length % 2 != 0)
            {
                hexString += " ";
            }

            var array = new byte[hexString.Length / 2];
            for (var i = 0; i < array.Length; i++)
            {
                array[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            }

            return array;
        }
        public static string GetQQNum(string six)
        {
            return Convert.ToInt64(six.Replace(" ", ""), 16).ToString();
        }
        public static ulong GetQQNumRetUint(string six)
        {
            return Convert.ToUInt64(six.Replace(" ", ""), 16);
        }

        /// <summary>
        ///     转换hex  长度不够前置补0
        /// </summary>
        /// <param name="qq"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string NumToHexString(long qq, int length = 8)
        {
            var text = Convert.ToString(qq, 16);
            if (text.Length == length)
            {
                return text;
            }

            if (text.Length > length)
            {
                return null;
            }

            var num = length - text.Length;
            var str = "";
            for (var i = 0; i < num; i++)
            {
                str += "0";
            }

            text = (str + text).ToUpper();
            var stringBuilder = new StringBuilder();
            for (var j = 0; j < text.Length; j++)
            {
                stringBuilder.Append(text[j]);
                if ((j + 1) % 2 == 0)
                {
                    stringBuilder.Append(" ");
                }
            }

            return stringBuilder.ToString();
        }
        public static string ConvertHexToString(string hexValue)
        {
            var bytes = HexStringToByteArray(hexValue);
            return Encoding.UTF8.GetString(bytes);
        }
     
    }
}
